package threadPoolDemo;

import java.util.concurrent.*;

/**
 * @author Ajie
 * @date 2022/1/17
 * @function  CacheThreadPool ::  Cache-->【Thread】 Pool
 * 背景知识；
 * CachedThreadPool：coreSize: 0，MaxSize: Integer.MAXVALUE。cache【是指的线程。】
 * CachedThreadPool 【场景】并发数高低非常不稳定的情景，所以核心线程数为0，最大线程数设置的非常大。
 *
 * SynchronousQueue：是一个不存储数据的阻塞队列，而是去维护一组阻塞的线程。
 * 每个take必须等待一个put 操作，反之每个put必须等待一个take 操作。
 * SynchronousQueue的size==0  jdk 1.8.0，
 *                 SynchronousQueue最重要的操作是offer与poll，他可以设置等待时间.
 *
 * String poll = synchronousQueue.poll(10, TimeUnit.MILLISECONDS); 当前线程， 超时 null
 * String take = synchronousQueue.take(); 当前线程，一直阻塞。
 *
 * boolean offer = synchronousQueue.offer("00", 50, TimeUnit.MILLISECONDS); 当前线程，超时 false
 * synchronousQueue.put("22");当前线程，一直阻塞
 *
 * ————————————————
 * 版权声明：本文为CSDN博主「韩运畅」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
 * 原文链接：https://blog.csdn.net/qq_41634872/article/details/109393730
 */
public class CacheThreadPoolDemo {
    public static void main(String[] args) {

        //new ThreadPoolExecutor(0, Integer.MAX_VALUE,
        //                                      60L, TimeUnit.SECONDS,
        //                                      new SynchronousQueue<Runnable>());
        ThreadPoolExecutor cachedThreadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();

        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("");
            }
        });

        SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>(false);

        System.out.println("size:"+synchronousQueue.size());
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
//                    Thread.sleep(3000);
                    while (true){
                        String poll = synchronousQueue.poll(10, TimeUnit.MILLISECONDS);
                        System.out.println("take:--start"+poll);
//                        String take = synchronousQueue.take();
//                        System.err.println("take:--result:"+take);
//                        Thread.sleep(3000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        //fs
        try {
            System.out.println("put:00");
            boolean offer = synchronousQueue.offer("00", 50, TimeUnit.MILLISECONDS);
            System.out.println("offer 00:"+offer+"  size:"+synchronousQueue.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("put:11--start"+synchronousQueue.size());
            boolean offer = synchronousQueue.offer("11", 50, TimeUnit.MILLISECONDS);
            System.out.println("offer 11:超时所以失败。"+offer);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("put:22--start");
            synchronousQueue.put("22");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
